package com.kaishengit.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.ProjectDao;
import com.kaishengit.dao.UserDao;
import com.kaishengit.dao.UserprojectDao;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.User;
import com.kaishengit.pojo.Userproject;
import com.kaishengit.util.DateUtil;
import com.kaishengit.util.StringUtil;

@Service
@Transactional
public class ProjectService {
	private ProjectDao projectDao;
	private UserDao userDao;
	private UserprojectDao userprojectDao;

	public void save(Project project, String selectname, String member) {
		User user = userDao.findById(Integer.parseInt(selectname));
		if (project == null || selectname == null || member == null
				|| user == null) {
			return;
		}

		project.setCreatetime(DateUtil.getDate());

		Userproject Userproject = new Userproject();
		Userproject.setUser(user);
		Userproject.setProject(project);
		Userproject.setRole("PM");

		projectDao.save(project);
		userprojectDao.save(Userproject);

		for (String member1 : StringUtil.strToList(member)) {
			User u = userDao.findName(member1);
			if (u != null) {
				Userproject uerproject1 = new Userproject();
				uerproject1.setUser(u);
				uerproject1.setProject(project);
				uerproject1.setRole("worker");
				userprojectDao.save(uerproject1);
			}
		}

	}

	public void update(Project project) {
		projectDao.update(project);
	}

	public void del(int id) {
		projectDao.del(id);
	}

	public Project findById(int id) {
		return projectDao.findById(id);
	}

	public List<Project> findAll() {
		return projectDao.findAll();
	}

	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Autowired
	public void setUserprojectDao(UserprojectDao userprojectDao) {
		this.userprojectDao = userprojectDao;
	}
}
